package com.rop.znyx.product.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rop.znyx.common.result.Result;
import com.rop.znyx.model.product.SkuInfo;
import com.rop.znyx.product.service.SkuInfoService;
import com.rop.znyx.vo.product.SkuInfoQueryVo;
import com.rop.znyx.vo.product.SkuInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * sku信息 前端控制器
 * </p>
 *
 * @author rop
 * @since 2023-06-24
 */

@Api(tags = "商品Sku管理")
@RestController
@RequestMapping("admin/product/skuInfo")
@RequiredArgsConstructor

public class SkuInfoController {

    private final SkuInfoService skuInfoService;

    /**
     * 获取sku分页列表
     */
    @ApiOperation("获取sku分页列表")
    @GetMapping("{page}/{limit}")
    public Result<IPage<SkuInfo>> index(@ApiParam(name = "page", value = "当前页数", required = true) @PathVariable("page") Long page,
                                        @ApiParam(name = "limit", value = "每页条数", required = true) @PathVariable("limit") Long limit,
                                        @ApiParam(name = "skuInfoQueryVo", value = "查询条件") SkuInfoQueryVo skuInfoQueryVo) {

        Page<SkuInfo> pageParam = new Page<>(page, limit);

        IPage<SkuInfo> skuInfoIPage = skuInfoService.selectByPage(pageParam, skuInfoQueryVo);

        return Result.ok(skuInfoIPage);
    }

    /**
     * 添加sku信息
     */
    @ApiOperation("添加sku信息")
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody SkuInfoVo skuInfoVo) {
        Boolean isSuccess = skuInfoService.saveSkuInfo(skuInfoVo);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 获取sku信息
     */
    @ApiOperation("获取sku信息")
    @GetMapping("get/{id}")
    public Result<SkuInfoVo> get(@PathVariable("id") Long id) {
        SkuInfoVo skuInfoVo = skuInfoService.getSkuInfoById(id);
        return Result.ok(skuInfoVo);
    }

    /**
     * 修改sku信息
     */
    @ApiOperation(value = "修改sku信息")
    @PutMapping("update")
    public Result<Boolean> updateById(@RequestBody SkuInfoVo skuInfoVo) {
        Boolean isSuccess = skuInfoService.updateSkuInfoById(skuInfoVo);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 删除sku
     */
    @ApiOperation("删除sku")
    @DeleteMapping("remove/{id}")
    public Result<Boolean> remove(@PathVariable Long id) {
        boolean isSuccess = skuInfoService.removeById(id);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 批量删除sku
     */
    @ApiOperation("批量删除sku")
    @DeleteMapping("batchRemove")
    public Result<Boolean> batchRemove(@RequestBody List<Long> ids) {
        boolean isSuccess = skuInfoService.removeByIds(ids);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 商品审核
     */
    @ApiOperation("商品审核")
    @GetMapping("check/{skuId}/{status}")
    public Result<Boolean> check(@PathVariable("skuId") Long skuId, @PathVariable("status") Integer status) {
        boolean isSuccess = skuInfoService.check(skuId, status);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 商品上架/下架
     */
    @ApiOperation("商品上架/下架")
    @GetMapping("publish/{skuId}/{status}")
    public Result<Boolean> publish(@PathVariable("skuId") Long skuId, @PathVariable("status") Integer status) {
        boolean isSuccess = skuInfoService.publish(skuId, status);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 是否新人专享
     */
    @ApiOperation("是否新人专享")
    @GetMapping("isNewPerson/{skuId}/{status}")
    public Result<Boolean> isNewPerson(@PathVariable("skuId") Long skuId, @PathVariable("status") Integer status) {
        boolean isSuccess = skuInfoService.isNewPerson(skuId, status);
        return isSuccess ? Result.ok() : Result.fail();
    }
}

